<?php

/**
 * This is the model class for table "template".
 *
 * The followings are the available columns in table 'template':
 * @property integer $tem_code
 * @property string $tem_name
 * @property string $tem_route
 * @property double $tem_price
 *
 * The followings are the available model relations:
 * @property Site[] $sites
 */
class Template extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Template the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'template';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tem_price', 'numerical'),
			array('tem_name,tem_type', 'required'),
			array('tem_name, tem_route', 'length', 'max'=>145),
			array('tem_image', 'file', 'types'=>'jpg, gif, png'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('tem_code, tem_name, tem_route, tem_price', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'sites' => array(self::HAS_MANY, 'Site', 'tem_code'),
			'templatehasuser' => array(self::HAS_MANY, 'UserHasTemplate', 'tem_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tem_code' => 'Tem Code',
			'tem_name' => 'Tem Name',
			'tem_route' => 'Tem Route',
			'tem_price' => 'Tem Price',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('tem_code',$this->tem_code);
		$criteria->compare('tem_name',$this->tem_name,true);
		$criteria->compare('tem_route',$this->tem_route,true);
		$criteria->compare('tem_price',$this->tem_price);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
		/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function searchHasUser()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		  
    $criteria->with='templatehasuser';
    $criteria->together=true;
		$criteria->compare('tem_code',$this->tem_code);
		$criteria->compare('tem_name',$this->tem_name,true);
		$criteria->compare('tem_route',$this->tem_route,true);
		$criteria->compare('tem_price',$this->tem_price);
		$criteria->condition = 'templatehasuser.usr_code='.Yii::app()->user->id.' AND templatehasuser.uht_state="activo"';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	 public static function enumItem($model,$attribute)
  {
          $attr=$attribute;
          //self::resolveName($model,$attr);
          preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
          foreach(explode(',', $matches[1]) as $value)
          {
                  $value=str_replace("'",null,$value);
                  $values[$value]=Yii::t('enumItem',$value);
          }
          
          return $values;
  }
  
}